package com.heakey.common.util;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Description excel工具类
 * @Author Bing
 * @Date 2024/12/4 20:48
 */
public class ExcelTools {
    /**
     * 导入excel
     * @param clazz
     * @param is
     * @return
     * @param <T>
     */
    public static <T> List<T> importExcel(Class<T> clazz, InputStream is) {
        ExcelUtil excelUtil = new ExcelUtil(clazz);
        return excelUtil.importExcelV2(is);
    }

    /**
     * 导出excel
     * @param response
     * @param sheetName
     * @param list
     * @param clazz
     * @param <T>
     * @throws IOException
     */
    public static <T> void exportList(HttpServletResponse response, String sheetName, List<T> list, Class<T> clazz) throws IOException {
        ExcelUtil excelUtil = new ExcelUtil(clazz);
        String fileName = sheetName+"-" + DatePattern.PURE_DATETIME_FORMAT.format(DateUtil.date());
        fileName = URLEncoder.encode(fileName, "utf-8");
        try {
            excelUtil.exportExcelV2(response,list, sheetName,fileName);
        } catch (Exception e) {
            throw new RuntimeException("导出异常",e);
        }
    }
}